﻿using System.Collections.Generic;
using System.Linq;
using CoreLibrary.Email.Enumeration;
using CoreLibrary.Email.Impl.Mandrill;

namespace CoreLibrary.Email {

    public class SendEmailResponse {

        public string ErrorMessage { get; set; }
        public string RciptId      { get; set; }
        public EmailState Status   { get; set; }

        public static SendEmailResponse Create(List<MailResult> results) {
            if (CollectionUtil.IsEmpty(results)) {
                return new SendEmailResponse {
                    RciptId      = string.Empty,
                    Status       = EmailState.Failed,
                    ErrorMessage = "No Result Found."
                };
            }

            var result = results.FirstOrDefault();
            if (result == null) {
                return new SendEmailResponse {
                       RciptId = string.Empty,
                       Status  = EmailState.Failed,
                       ErrorMessage = "No Result Returned",
                };
            }

            var status = EmailState.ConvertMandrillStatus(result.Status);
            var errorMessage = string.Empty;
            if (status.Equals(EmailState.Failed)) {
                errorMessage = string.Format("sending email to {0} failed, status is {1}", result.Email, result.Status);
            }

            return new SendEmailResponse {
                   RciptId = Util.GetString(result.Id),
                   Status  = status,
                   ErrorMessage = errorMessage
            };

        }

    }
}